<?php
class App_User_Model_User extends Main_Db_Table_Abstract
{
	/**
	 * @var string
	 */
	protected $_name = 'user';
	
	/**
	 * @var string
	 */
	protected $_primary = 'id';
	
	/**
	 * @var array
	 */
	protected $_fields = array(
		'id',
	    'login',  
   		'password', 
   		'email',    
   		'role_id',  
   		'f_active', 
	);
	
	/**
	 * 
	 * @var App_User_Model_UserI18n
	 */
	protected $_modelUserI18n;	
	
	/**
	 * @return void
	 */
	public function init() 
	{
		$this->_modelUserI18n = Main_Model::factory('User_Model_UserI18n');
	}

	/**
	 * @param array $data
	 * @return bool
	 */
	public function insertItem($data) 
	{
		$dataOk = $this->beforeSave($data, array(
			'login',  
   			'password', 
   			'email',    
   			'role_id',  
   			'f_active',
		), array(
			'login',
			'password', 
   			'email',
		));
		
		$result = $this->insert($dataOk);
				
		$data['user_id'] = $this->lastInsertId(); 
		$res1 = $this->_modelUserI18n->insertItem($data);
		
		return $result && $res1;
	}
	
	/**
	 * @param array $data
	 * @param int $id
	 * @return bool
	 */
	public function updateItem($data, $id)
	{
		$id = $this->escape($id);
		$dataOk = $this->beforeSave($data, array(
			'login',  
   			'password', 
   			'email',    
   			'role_id',  
   			'f_active',
		), array(
			'login',
			'password', 
   			'email',
		));
    	
		$res1 = $this->_modelUserI18n->updateItem($data, $id);  	
		$res2 = $this->update($dataOk, "id={$id}");
		
    	return $res1 && $res2;
	}
	
	/**
	 * @param int $id
	 * @param int $langId
	 * @return array
	 */
	public function getItemById($id, $langId) 
	{
		$id = $this->escape($id);
		
		$sql = "
			SELECT 
				u.id
				, u.login
				, u.password
				, u.email
				, u.f_active
				, r.id role_id
				, r_i18n.name role_name
				, u_i18n.first_name
				, u_i18n.second_name
				, u_i18n.middle_name
			FROM
				user u
			INNER JOIN
				user_i18n u_i18n
				ON
					(u.id=u_i18n.user_id AND u_i18n.lang_id={$langId})		
			INNER JOIN
				role r
				ON
					(r.id=u.role_id)
			INNER JOIN		
				role_i18n r_i18n
					ON
						(r.id=r_i18n.role_id AND r_i18n.lang_id={$langId})
			WHERE
				u.id={$id}		
		";
				
		$list = $this->getAdapter()->fetchRow($sql);

		return $list;
	}

	/**
	 * @param string $login
	 * @param int $langId
	 * @return array
	 */
	public function getItemByLogin($login, $langId) 
	{
		$id = $this->escape($id);
		
		$sql = "
			SELECT 
				u.id
				, u.login
				, u.password
				, u.email
				, u.f_active
				, r.id role_id
				, r_i18n.name role_name
				, u_i18n.first_name
				, u_i18n.second_name
				, u_i18n.middle_name
			FROM
				user u
			INNER JOIN
				user_i18n u_i18n
				ON
					(u.id=u_i18n.user_id AND u_i18n.lang_id={$langId})		
			INNER JOIN
				role r
				ON
					(r.id=u.role_id)
			INNER JOIN		
				role_i18n r_i18n
					ON
						(r.id=r_i18n.role_id AND r_i18n.lang_id={$langId})
			WHERE
				u.login='{$login}'		
		";
				
		$list = $this->getAdapter()->fetchRow($sql);

		return $list;
	}
	
	
	/**
	 * @param int $id
	 * @return bool
	 */
	public function deleteItem($id) 
	{
		$id = $this->escape($id);
		if ($this->_modelUserI18n->deleteItemByUserId($id)) {
			return $this->delete("id={$id}");
		} else {
			return false;
		}	
	}
	
} 
